package com.peanut.web.filter;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.filter.OncePerRequestFilter;

import com.peanut.util.http.HttpRequestUtil;

/**
 * @author weigangqiu
 *
 */
public class CorsFilter extends OncePerRequestFilter {

  Logger logger = LoggerFactory.getLogger(getClass());

  /**
   * 设置请求
   */
  @Override
  protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
      throws ServletException, IOException {
    logger.info("请求HEAD: " + this.getAllHeaders(request));
    String ip = HttpRequestUtil.getRemoteIpWithHttp(request);
    logger.info("GET IP ADDRESS: " + ip);
    response.setContentType(MediaType.APPLICATION_JSON_VALUE);
    response.addHeader("Access-Control-Allow-Origin", ip);
    response.addHeader("Access-Control-Allow-Credentials", "true");
    if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
      response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
      response.addHeader("Access-Control-Allow-Headers",
          "X-Requested-With,Origin,Content-Type,Accept,X-Auth-Token,X-Data-Type,X-Forwarded-For");
    }
    filterChain.doFilter(request, response);
  }

  public String getAllHeaders(HttpServletRequest request) {
    StringBuffer sb = new StringBuffer();
    Enumeration<String> headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
      String key = (String) headerNames.nextElement();
      String value = request.getHeader(key);
      sb.append(key).append("=").append(value).append(";");
    }
    return sb.toString();
  }
}
